#include <stdio.h>
#include <stdlib.h>
#include <conio.h> 
#include <windows.h>

//宏定义定义画面大小 
#define high  25
#define width 45

void gotoxy(int x,int y)		//光标移动到（x，y）的位置 
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle,pos); 
} 

void HideCursor()				//隐藏光标 
{
	CONSOLE_CURSOR_INFO cursor_info={1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

//	全局变量 
int cells[high][width];

void startup()					//数据初始化
{
	int i,j;
	
	for(i=0;i<=high;i++)
	for(j=0;j<=width;j++)
		cells[i][j]=rand() % 2;
	
	HideCursor();
}

void show()						//显示界面 
{
	int i,j;
	
	gotoxy(0,0);				//光标移动到原点位置，以下重新清屏 
	
	for(i=0;i<=high;i++)
	{
		for(j=0;j<=width;j++)
		{
			if(cells[i][j] == 1)
			printf("*");		//输出细胞 
			else
			printf(" ");		//输出空格 
		}
		printf("\n"); 
	}
}

void updatewith()				//与用户输出有关 
{
	
}

void updatewithout()			//与用户输出无关 
{
	Sleep(80);					//减慢运行速度 

	int i,j;
	int nearcount=0;
	int temp[high][width];
	
	for(i=0;i<high-1;i++)
	for(j=0;j<width-1;j++)
		temp[i][j] = 1;
	
	for(i=0;i<high-1;i++)
	{
		for(j=0;j<width-1;j++)
		{
			nearcount = cells[i-1][j-1] + cells[i-1][j] + cells[i-1][j+1]
					  + cells[i][j-1]   + cells[i][j]   + cells[i][j+1]
					  + cells[i+1][j-1] + cells[i+1][j] + cells[i+1][j+1];
			
			if(nearcount == 3)
			temp[i][j] = 1;
			else if(nearcount == 2)
			temp[i][j] = cells[i][j];
			else
			temp[i][j] = 0;
		}
	}
	
	for(i=0;i<high-1;i++)
	for(j=0;j<width-1;j++)
		cells[i][j] = temp[i][j];
}
 
int main() 
{
	startup();					//数据初始化
	while(1)					//游戏循环执行 
	{
		show();					//显示界面 
		updatewith();			//与用户输出有关 
		updatewithout();		//与用户输出无关 
	} 
	return 0;
} 